package com.czr.controller.blog;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czr.config.userlocal.UserThreadLcal;
import com.czr.entity.Blog;
import com.czr.entity.User;
import com.czr.enums.StatusEnum;
import com.czr.service.blog.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * Description:
 * Author: czr czr
 * Version: 1.0
 * Create Date Time: 2021/11/19 9:28.
 * Update Date Time:
 *
 * @see
 */
@Controller
public class BlogController {

    @Autowired
    private BlogService blogService;



    /**
     * 增加文章，存在则修改
     * @param blog
     * @return
     */
    @PostMapping("/api/blog/saveupdate")
    @ResponseBody
    public Boolean saveupdate(@RequestBody Blog blog) {
        // 这里判断的原因是：只有保存的时候，才有初始值，
        // 如果你不判断修改如果别人已经删除的。你会恢复会去的
        if (blog.getId() == null) {
            blog.setStatus(StatusEnum.STATUS);
            blog.setIsDelete(0);
            User user = UserThreadLcal.get();
            blog.setUserId(user.getId());
        }
        boolean blogflag = blogService.saveOrUpdate(blog);
        return blogflag ? blogflag : null;
    }

    @GetMapping("/blog/add")
    public String blogAdd() {

        return "blog/add";
    }

    /**
     * 查询文章内容信息
     * @param pageNo
     * @param pageSize
     * @param cid
     * @param sortnum
     * @return Page<Blog>
     */
    @GetMapping("/api/blog/load")
    @ResponseBody
    public Page<Blog> blogLoad(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                               @RequestParam(required = false, defaultValue = "0") Integer cid,
                               @RequestParam(required = false, defaultValue = "0") Integer sortnum) {
        // 设置分页起始页和每页显示多少条
        Page<Blog> blogPage = new Page<>(pageNo, pageSize);
        // 设置条件
        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        // 查询 status = 1 发布且isdelete = 0未删除，  根据创建时间排序 desc
        queryWrapper.eq(Blog::getStatus, 1);
        // 分类id  0代表查询全部 如果是0那就不拼接
        queryWrapper.eq(cid != null && !cid.equals(0) ,Blog::getCategoryId, cid);

        if (sortnum.equals(0) || sortnum.equals(2)) {
            queryWrapper.orderByDesc(Blog::getCreateTime);
        } else if (sortnum.equals(1)) {
            queryWrapper.orderByAsc(Blog::getCreateTime);
        } else if (sortnum.equals(3)) {
            // 根据条件去查询即可
        }

        // 放到page集合中传到前端
        Page<Blog> page = blogService.page(blogPage, queryWrapper);

        return page;
    }

    /**
     * 编辑
     * @param blogId
     * @param model
     * @return
     */
    @GetMapping("/blog/edit/{blogId}")
    public String toedit(@PathVariable("blogId") Integer blogId, Model model) {
        model.addAttribute("blogId", blogId);
        return "/blog/add";
    }
    /**
     * 根据id查询文章信息
     * @param blogId
     * @return
     */
    @GetMapping("/api/blog/get/{blogId}")
    @ResponseBody
    public Blog geBlog(@PathVariable("blogId")Integer blogId) {
        Optional.ofNullable(blogId).orElseThrow(() -> new RuntimeException("文章找不到!!!"));
        Blog blog = blogService.getById(blogId);
        Optional.ofNullable(blog).orElseThrow(() -> new RuntimeException("文章找不到!!!"));
        return blog;
    }



}
